import java.awt.*;

public class Player extends EObject {
    private boolean isSeleted = false;
    static int map[][] = Maps.getMap();
    static int N = 1;//北
    static int S = -1;//南
    static int W = -2;//西
    static int E = 2;//东

    public boolean isSeleted() {
        return isSeleted;
    }

    public void setSeleted(boolean seleted) {
        isSeleted = seleted;
    }

    public Player(int x, int y, int tempposX, int tempposY, int nowDirection, int futureDirection) {
        super(x, y, tempposX, tempposY, nowDirection, futureDirection);
        this.x = x;
        this.y = y;
        this.tempposX = tempposX;
        this.tempposY = tempposY;
        this.nowDirection = nowDirection;
        this.futureDirection = futureDirection;
    }

    public void wayPoint() {
        do {
            do {
                do {
                    int t = (int) (Math.random() * 4);
                    switch (t) {
                        case 0:
                            futureDirection = N;
                            break;
                        case 1:
                            futureDirection = S;
                            break;
                        case 2:
                            futureDirection = W;
                            break;
                        case 3:
                            futureDirection = E;
                            break;
                    }
                } while (nowDirection + futureDirection == 0);
                tempposX = x;
                tempposY = y;
                if (futureDirection == 1) {
                    tempposY -= 1;
                }
                if (futureDirection == -1) {
                    tempposY += 1;
                }
                if (futureDirection == 2) {
                    tempposX += 1;
                }
                if (futureDirection == -2) {
                    tempposX -= 1;
                }
            } while (tempposX < 0 || tempposX >= map.length || tempposY < 0 || tempposY >= map[map.length - 1].length);
        } while (map[tempposX][tempposY] != 1);
        y = tempposY;
        x = tempposX;
        // System.out.println("正posX:"+x + "正posY:"+y+"正map[posX][posY]:"+map[x][y]);
        nowDirection = futureDirection;
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void tick() {
        wayPoint();


    }

    public void playMove(java.awt.event.MouseEvent e) {
        if (e.getX() / 60 >= 0 && e.getX() / 60 < Maps.map[0].length && e.getY() / 60 >= 0 && e.getY() / 60 < Maps.map.length) {
            if (e.getX() / 60 == getY() &&
                    e.getY() / 60 == getX()) {
                if (isSeleted()) {
                    setSeleted(false);
                } else {
                    setSeleted(true);
                }
            } else {
                if (isSeleted()) {
                    if (e.getY() / 60 >= getX() - 2 &&
                            e.getY() / 60 <= getX() + 2 &&
                            e.getX() / 60 >= getY() - 2 &&
                            e.getX() / 60 <= getY() + 2) {
                        setY(e.getX() / 60);
                        setX(e.getY() / 60);
                        setSeleted(false);
                    }
                }
            }
        }
    }

    public void render(Graphics g) {
        if (isSeleted) {
            g.setColor(new Color(255, 33, 60, 50));
            for (int i = getX() - 2; i <= getX() + 2; i++)
                for (int j = getY() - 2; j <= getY() + 2; j++) {
                    if (i >= 0 && i < Maps.map.length && j >= 0 && j < Maps.map[0].length) {
                        g.fillRect(j * 60, i * 60, 60, 60);
                    }
                }
        }
        g.setColor(Color.blue);
        g.fillRect(y * 60, x * 60, 60, 60);
    }
}
